/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dale2507.gates.gate;

import dale2507.gates.events.GateStatusChangeEvent;
import dale2507.gates.events.GateTravelEvent;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockRedstoneEvent;

/**
 *
 * @author Dale
 */
public class KeyIris extends IIris {
    
    KeyIris(Gate gate, Block irisPower) {
        super(gate, irisPower);
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    @Override
    public void onGateStatusChange(GateStatusChangeEvent event) {
        super.onGateStatusChange(event);
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    @Override
    public void onIrisPowerChanged(BlockRedstoneEvent event) {
        super.onIrisPowerChanged(event);
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    @Override
    public void onIrisBroken(BlockBreakEvent event) {
        super.onIrisBroken(event);
    }
    
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
    public void onTraveller(GateTravelEvent event) {
        if (event.getDestination().equals(this.getGate()) && this.isEngaged()) {
            super.onTraveller(event);
            
            Player p = null;
            if (event.getEntity() instanceof Player) p = (Player) event.getEntity();
            if (event.getEntity() instanceof Vehicle) {
                Vehicle v = (Vehicle) event.getEntity();
                if (v.getPassenger() != null && v.getPassenger() instanceof Player)
                    p = (Player) event.getEntity();
            }
            
            if (p != null) {
                if (this.getGate().getSettings().getIrisKey().equals(p.getItemInHand().getType()))
                    this.setEngaged(false);
            }
        }
    }
    
}
